<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>27.7. tracemalloc — Trace memory allocations &mdash; Python 3.4.3 documentation</title>
    
    <link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '3.4.3',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/sidebar.js"></script>
    <link rel="search" type="application/opensearchdescription+xml"
          title="Search within Python 3.4.3 documentation"
          href="../_static/opensearch.xml"/>
    <link rel="author" title="About these documents" href="../about.html" />
    <link rel="copyright" title="Copyright" href="../copyright.html" />
    <link rel="top" title="Python 3.4.3 documentation" href="../index.html" />
    <link rel="up" title="27. Debugging and Profiling" href="debug.html" />
    <link rel="next" title="28. Software Packaging and Distribution" href="distribution.html" />
    <link rel="prev" title="27.6. trace — Trace or track Python statement execution" href="trace.html" />
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
    <script type="text/javascript" src="../_static/copybutton.js"></script>
    <script type="text/javascript" src="../_static/version_switch.js"></script>
    
 

  </head>
  <body>  
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="distribution.html" title="28. Software Packaging and Distribution"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="trace.html" title="27.6. trace — Trace or track Python statement execution"
             accesskey="P">previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="https://www.python.org/">Python</a> &raquo;</li>
        <li>
          <span class="version_switcher_placeholder">3.4.3</span>
          <a href="../index.html">Documentation</a> &raquo;
        </li>

          <li><a href="index.html" >The Python Standard Library</a> &raquo;</li>
          <li><a href="debug.html" accesskey="U">27. Debugging and Profiling</a> &raquo;</li> 
      </ul>
    </div>    

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="module-tracemalloc">
<span id="tracemalloc-trace-memory-allocations"></span><h1>27.7. <a class="reference internal" href="#module-tracemalloc" title="tracemalloc: Trace memory allocations."><tt class="xref py py-mod docutils literal"><span class="pre">tracemalloc</span></tt></a> &#8212; Trace memory allocations<a class="headerlink" href="#module-tracemalloc" title="Permalink to this headline">¶</a></h1>
<div class="versionadded">
<p><span class="versionmodified">New in version 3.4.</span></p>
</div>
<p>The tracemalloc module is a debug tool to trace memory blocks allocated by
Python. It provides the following information:</p>
<ul class="simple">
<li>Traceback where an object was allocated</li>
<li>Statistics on allocated memory blocks per filename and per line number:
total size, number and average size of allocated memory blocks</li>
<li>Compute the differences between two snapshots to detect memory leaks</li>
</ul>
<p>To trace most memory blocks allocated by Python, the module should be started
as early as possible by setting the <span class="target" id="index-0"></span><a class="reference internal" href="../using/cmdline.html#envvar-PYTHONTRACEMALLOC"><tt class="xref std std-envvar docutils literal"><span class="pre">PYTHONTRACEMALLOC</span></tt></a> environment
variable to <tt class="docutils literal"><span class="pre">1</span></tt>, or by using <a class="reference internal" href="../using/cmdline.html#cmdoption-X"><em class="xref std std-option">-X</em></a> <tt class="docutils literal"><span class="pre">tracemalloc</span></tt> command line
option. The <a class="reference internal" href="#tracemalloc.start" title="tracemalloc.start"><tt class="xref py py-func docutils literal"><span class="pre">tracemalloc.start()</span></tt></a> function can be called at runtime to
start tracing Python memory allocations.</p>
<p>By default, a trace of an allocated memory block only stores the most recent
frame (1 frame). To store 25 frames at startup: set the
<span class="target" id="index-1"></span><a class="reference internal" href="../using/cmdline.html#envvar-PYTHONTRACEMALLOC"><tt class="xref std std-envvar docutils literal"><span class="pre">PYTHONTRACEMALLOC</span></tt></a> environment variable to <tt class="docutils literal"><span class="pre">25</span></tt>, or use the
<a class="reference internal" href="../using/cmdline.html#cmdoption-X"><em class="xref std std-option">-X</em></a> <tt class="docutils literal"><span class="pre">tracemalloc=25</span></tt> command line option.</p>
<div class="section" id="examples">
<h2>27.7.1. Examples<a class="headerlink" href="#examples" title="Permalink to this headline">¶</a></h2>
<div class="section" id="display-the-top-10">
<h3>27.7.1.1. Display the top 10<a class="headerlink" href="#display-the-top-10" title="Permalink to this headline">¶</a></h3>
<p>Display the 10 files allocating the most memory:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">tracemalloc</span>

<span class="n">tracemalloc</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>

<span class="c"># ... run your application ...</span>

<span class="n">snapshot</span> <span class="o">=</span> <span class="n">tracemalloc</span><span class="o">.</span><span class="n">take_snapshot</span><span class="p">()</span>
<span class="n">top_stats</span> <span class="o">=</span> <span class="n">snapshot</span><span class="o">.</span><span class="n">statistics</span><span class="p">(</span><span class="s">&#39;lineno&#39;</span><span class="p">)</span>

<span class="nb">print</span><span class="p">(</span><span class="s">&quot;[ Top 10 ]&quot;</span><span class="p">)</span>
<span class="k">for</span> <span class="n">stat</span> <span class="ow">in</span> <span class="n">top_stats</span><span class="p">[:</span><span class="mi">10</span><span class="p">]:</span>
    <span class="nb">print</span><span class="p">(</span><span class="n">stat</span><span class="p">)</span>
</pre></div>
</div>
<p>Example of output of the Python test suite:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="p">[</span> <span class="n">Top</span> <span class="mi">10</span> <span class="p">]</span>
<span class="o">&lt;</span><span class="n">frozen</span> <span class="n">importlib</span><span class="o">.</span><span class="n">_bootstrap</span><span class="o">&gt;</span><span class="p">:</span><span class="mi">716</span><span class="p">:</span> <span class="n">size</span><span class="o">=</span><span class="mi">4855</span> <span class="n">KiB</span><span class="p">,</span> <span class="n">count</span><span class="o">=</span><span class="mi">39328</span><span class="p">,</span> <span class="n">average</span><span class="o">=</span><span class="mi">126</span> <span class="n">B</span>
<span class="o">&lt;</span><span class="n">frozen</span> <span class="n">importlib</span><span class="o">.</span><span class="n">_bootstrap</span><span class="o">&gt;</span><span class="p">:</span><span class="mi">284</span><span class="p">:</span> <span class="n">size</span><span class="o">=</span><span class="mi">521</span> <span class="n">KiB</span><span class="p">,</span> <span class="n">count</span><span class="o">=</span><span class="mi">3199</span><span class="p">,</span> <span class="n">average</span><span class="o">=</span><span class="mi">167</span> <span class="n">B</span>
<span class="o">/</span><span class="n">usr</span><span class="o">/</span><span class="n">lib</span><span class="o">/</span><span class="n">python3</span><span class="o">.</span><span class="mi">4</span><span class="o">/</span><span class="n">collections</span><span class="o">/</span><span class="n">__init__</span><span class="o">.</span><span class="n">py</span><span class="p">:</span><span class="mi">368</span><span class="p">:</span> <span class="n">size</span><span class="o">=</span><span class="mi">244</span> <span class="n">KiB</span><span class="p">,</span> <span class="n">count</span><span class="o">=</span><span class="mi">2315</span><span class="p">,</span> <span class="n">average</span><span class="o">=</span><span class="mi">108</span> <span class="n">B</span>
<span class="o">/</span><span class="n">usr</span><span class="o">/</span><span class="n">lib</span><span class="o">/</span><span class="n">python3</span><span class="o">.</span><span class="mi">4</span><span class="o">/</span><span class="n">unittest</span><span class="o">/</span><span class="n">case</span><span class="o">.</span><span class="n">py</span><span class="p">:</span><span class="mi">381</span><span class="p">:</span> <span class="n">size</span><span class="o">=</span><span class="mi">185</span> <span class="n">KiB</span><span class="p">,</span> <span class="n">count</span><span class="o">=</span><span class="mi">779</span><span class="p">,</span> <span class="n">average</span><span class="o">=</span><span class="mi">243</span> <span class="n">B</span>
<span class="o">/</span><span class="n">usr</span><span class="o">/</span><span class="n">lib</span><span class="o">/</span><span class="n">python3</span><span class="o">.</span><span class="mi">4</span><span class="o">/</span><span class="n">unittest</span><span class="o">/</span><span class="n">case</span><span class="o">.</span><span class="n">py</span><span class="p">:</span><span class="mi">402</span><span class="p">:</span> <span class="n">size</span><span class="o">=</span><span class="mi">154</span> <span class="n">KiB</span><span class="p">,</span> <span class="n">count</span><span class="o">=</span><span class="mi">378</span><span class="p">,</span> <span class="n">average</span><span class="o">=</span><span class="mi">416</span> <span class="n">B</span>
<span class="o">/</span><span class="n">usr</span><span class="o">/</span><span class="n">lib</span><span class="o">/</span><span class="n">python3</span><span class="o">.</span><span class="mi">4</span><span class="o">/</span><span class="n">abc</span><span class="o">.</span><span class="n">py</span><span class="p">:</span><span class="mi">133</span><span class="p">:</span> <span class="n">size</span><span class="o">=</span><span class="mf">88.7</span> <span class="n">KiB</span><span class="p">,</span> <span class="n">count</span><span class="o">=</span><span class="mi">347</span><span class="p">,</span> <span class="n">average</span><span class="o">=</span><span class="mi">262</span> <span class="n">B</span>
<span class="o">&lt;</span><span class="n">frozen</span> <span class="n">importlib</span><span class="o">.</span><span class="n">_bootstrap</span><span class="o">&gt;</span><span class="p">:</span><span class="mi">1446</span><span class="p">:</span> <span class="n">size</span><span class="o">=</span><span class="mf">70.4</span> <span class="n">KiB</span><span class="p">,</span> <span class="n">count</span><span class="o">=</span><span class="mi">911</span><span class="p">,</span> <span class="n">average</span><span class="o">=</span><span class="mi">79</span> <span class="n">B</span>
<span class="o">&lt;</span><span class="n">frozen</span> <span class="n">importlib</span><span class="o">.</span><span class="n">_bootstrap</span><span class="o">&gt;</span><span class="p">:</span><span class="mi">1454</span><span class="p">:</span> <span class="n">size</span><span class="o">=</span><span class="mf">52.0</span> <span class="n">KiB</span><span class="p">,</span> <span class="n">count</span><span class="o">=</span><span class="mi">25</span><span class="p">,</span> <span class="n">average</span><span class="o">=</span><span class="mi">2131</span> <span class="n">B</span>
<span class="o">&lt;</span><span class="n">string</span><span class="o">&gt;</span><span class="p">:</span><span class="mi">5</span><span class="p">:</span> <span class="n">size</span><span class="o">=</span><span class="mf">49.7</span> <span class="n">KiB</span><span class="p">,</span> <span class="n">count</span><span class="o">=</span><span class="mi">148</span><span class="p">,</span> <span class="n">average</span><span class="o">=</span><span class="mi">344</span> <span class="n">B</span>
<span class="o">/</span><span class="n">usr</span><span class="o">/</span><span class="n">lib</span><span class="o">/</span><span class="n">python3</span><span class="o">.</span><span class="mi">4</span><span class="o">/</span><span class="n">sysconfig</span><span class="o">.</span><span class="n">py</span><span class="p">:</span><span class="mi">411</span><span class="p">:</span> <span class="n">size</span><span class="o">=</span><span class="mf">48.0</span> <span class="n">KiB</span><span class="p">,</span> <span class="n">count</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">average</span><span class="o">=</span><span class="mf">48.0</span> <span class="n">KiB</span>
</pre></div>
</div>
<p>We can see that Python loaded <tt class="docutils literal"><span class="pre">4.8</span> <span class="pre">MiB</span></tt> data (bytecode and constants) from
modules and that the <a class="reference internal" href="collections.html#module-collections" title="collections: Container datatypes"><tt class="xref py py-mod docutils literal"><span class="pre">collections</span></tt></a> module allocated <tt class="docutils literal"><span class="pre">244</span> <span class="pre">KiB</span></tt> to build
<a class="reference internal" href="collections.html#collections.namedtuple" title="collections.namedtuple"><tt class="xref py py-class docutils literal"><span class="pre">namedtuple</span></tt></a> types.</p>
<p>See <a class="reference internal" href="#tracemalloc.Snapshot.statistics" title="tracemalloc.Snapshot.statistics"><tt class="xref py py-meth docutils literal"><span class="pre">Snapshot.statistics()</span></tt></a> for more options.</p>
</div>
<div class="section" id="compute-differences">
<h3>27.7.1.2. Compute differences<a class="headerlink" href="#compute-differences" title="Permalink to this headline">¶</a></h3>
<p>Take two snapshots and display the differences:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">tracemalloc</span>
<span class="n">tracemalloc</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>
<span class="c"># ... start your application ...</span>

<span class="n">snapshot1</span> <span class="o">=</span> <span class="n">tracemalloc</span><span class="o">.</span><span class="n">take_snapshot</span><span class="p">()</span>
<span class="c"># ... call the function leaking memory ...</span>
<span class="n">snapshot2</span> <span class="o">=</span> <span class="n">tracemalloc</span><span class="o">.</span><span class="n">take_snapshot</span><span class="p">()</span>

<span class="n">top_stats</span> <span class="o">=</span> <span class="n">snapshot2</span><span class="o">.</span><span class="n">compare_to</span><span class="p">(</span><span class="n">snapshot1</span><span class="p">,</span> <span class="s">&#39;lineno&#39;</span><span class="p">)</span>

<span class="nb">print</span><span class="p">(</span><span class="s">&quot;[ Top 10 differences ]&quot;</span><span class="p">)</span>
<span class="k">for</span> <span class="n">stat</span> <span class="ow">in</span> <span class="n">top_stats</span><span class="p">[:</span><span class="mi">10</span><span class="p">]:</span>
    <span class="nb">print</span><span class="p">(</span><span class="n">stat</span><span class="p">)</span>
</pre></div>
</div>
<p>Example of output before/after running some tests of the Python test suite:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="p">[</span> <span class="n">Top</span> <span class="mi">10</span> <span class="n">differences</span> <span class="p">]</span>
<span class="o">&lt;</span><span class="n">frozen</span> <span class="n">importlib</span><span class="o">.</span><span class="n">_bootstrap</span><span class="o">&gt;</span><span class="p">:</span><span class="mi">716</span><span class="p">:</span> <span class="n">size</span><span class="o">=</span><span class="mi">8173</span> <span class="n">KiB</span> <span class="p">(</span><span class="o">+</span><span class="mi">4428</span> <span class="n">KiB</span><span class="p">),</span> <span class="n">count</span><span class="o">=</span><span class="mi">71332</span> <span class="p">(</span><span class="o">+</span><span class="mi">39369</span><span class="p">),</span> <span class="n">average</span><span class="o">=</span><span class="mi">117</span> <span class="n">B</span>
<span class="o">/</span><span class="n">usr</span><span class="o">/</span><span class="n">lib</span><span class="o">/</span><span class="n">python3</span><span class="o">.</span><span class="mi">4</span><span class="o">/</span><span class="n">linecache</span><span class="o">.</span><span class="n">py</span><span class="p">:</span><span class="mi">127</span><span class="p">:</span> <span class="n">size</span><span class="o">=</span><span class="mi">940</span> <span class="n">KiB</span> <span class="p">(</span><span class="o">+</span><span class="mi">940</span> <span class="n">KiB</span><span class="p">),</span> <span class="n">count</span><span class="o">=</span><span class="mi">8106</span> <span class="p">(</span><span class="o">+</span><span class="mi">8106</span><span class="p">),</span> <span class="n">average</span><span class="o">=</span><span class="mi">119</span> <span class="n">B</span>
<span class="o">/</span><span class="n">usr</span><span class="o">/</span><span class="n">lib</span><span class="o">/</span><span class="n">python3</span><span class="o">.</span><span class="mi">4</span><span class="o">/</span><span class="n">unittest</span><span class="o">/</span><span class="n">case</span><span class="o">.</span><span class="n">py</span><span class="p">:</span><span class="mi">571</span><span class="p">:</span> <span class="n">size</span><span class="o">=</span><span class="mi">298</span> <span class="n">KiB</span> <span class="p">(</span><span class="o">+</span><span class="mi">298</span> <span class="n">KiB</span><span class="p">),</span> <span class="n">count</span><span class="o">=</span><span class="mi">589</span> <span class="p">(</span><span class="o">+</span><span class="mi">589</span><span class="p">),</span> <span class="n">average</span><span class="o">=</span><span class="mi">519</span> <span class="n">B</span>
<span class="o">&lt;</span><span class="n">frozen</span> <span class="n">importlib</span><span class="o">.</span><span class="n">_bootstrap</span><span class="o">&gt;</span><span class="p">:</span><span class="mi">284</span><span class="p">:</span> <span class="n">size</span><span class="o">=</span><span class="mi">1005</span> <span class="n">KiB</span> <span class="p">(</span><span class="o">+</span><span class="mi">166</span> <span class="n">KiB</span><span class="p">),</span> <span class="n">count</span><span class="o">=</span><span class="mi">7423</span> <span class="p">(</span><span class="o">+</span><span class="mi">1526</span><span class="p">),</span> <span class="n">average</span><span class="o">=</span><span class="mi">139</span> <span class="n">B</span>
<span class="o">/</span><span class="n">usr</span><span class="o">/</span><span class="n">lib</span><span class="o">/</span><span class="n">python3</span><span class="o">.</span><span class="mi">4</span><span class="o">/</span><span class="n">mimetypes</span><span class="o">.</span><span class="n">py</span><span class="p">:</span><span class="mi">217</span><span class="p">:</span> <span class="n">size</span><span class="o">=</span><span class="mi">112</span> <span class="n">KiB</span> <span class="p">(</span><span class="o">+</span><span class="mi">112</span> <span class="n">KiB</span><span class="p">),</span> <span class="n">count</span><span class="o">=</span><span class="mi">1334</span> <span class="p">(</span><span class="o">+</span><span class="mi">1334</span><span class="p">),</span> <span class="n">average</span><span class="o">=</span><span class="mi">86</span> <span class="n">B</span>
<span class="o">/</span><span class="n">usr</span><span class="o">/</span><span class="n">lib</span><span class="o">/</span><span class="n">python3</span><span class="o">.</span><span class="mi">4</span><span class="o">/</span><span class="n">http</span><span class="o">/</span><span class="n">server</span><span class="o">.</span><span class="n">py</span><span class="p">:</span><span class="mi">848</span><span class="p">:</span> <span class="n">size</span><span class="o">=</span><span class="mf">96.0</span> <span class="n">KiB</span> <span class="p">(</span><span class="o">+</span><span class="mf">96.0</span> <span class="n">KiB</span><span class="p">),</span> <span class="n">count</span><span class="o">=</span><span class="mi">1</span> <span class="p">(</span><span class="o">+</span><span class="mi">1</span><span class="p">),</span> <span class="n">average</span><span class="o">=</span><span class="mf">96.0</span> <span class="n">KiB</span>
<span class="o">/</span><span class="n">usr</span><span class="o">/</span><span class="n">lib</span><span class="o">/</span><span class="n">python3</span><span class="o">.</span><span class="mi">4</span><span class="o">/</span><span class="n">inspect</span><span class="o">.</span><span class="n">py</span><span class="p">:</span><span class="mi">1465</span><span class="p">:</span> <span class="n">size</span><span class="o">=</span><span class="mf">83.5</span> <span class="n">KiB</span> <span class="p">(</span><span class="o">+</span><span class="mf">83.5</span> <span class="n">KiB</span><span class="p">),</span> <span class="n">count</span><span class="o">=</span><span class="mi">109</span> <span class="p">(</span><span class="o">+</span><span class="mi">109</span><span class="p">),</span> <span class="n">average</span><span class="o">=</span><span class="mi">784</span> <span class="n">B</span>
<span class="o">/</span><span class="n">usr</span><span class="o">/</span><span class="n">lib</span><span class="o">/</span><span class="n">python3</span><span class="o">.</span><span class="mi">4</span><span class="o">/</span><span class="n">unittest</span><span class="o">/</span><span class="n">mock</span><span class="o">.</span><span class="n">py</span><span class="p">:</span><span class="mi">491</span><span class="p">:</span> <span class="n">size</span><span class="o">=</span><span class="mf">77.7</span> <span class="n">KiB</span> <span class="p">(</span><span class="o">+</span><span class="mf">77.7</span> <span class="n">KiB</span><span class="p">),</span> <span class="n">count</span><span class="o">=</span><span class="mi">143</span> <span class="p">(</span><span class="o">+</span><span class="mi">143</span><span class="p">),</span> <span class="n">average</span><span class="o">=</span><span class="mi">557</span> <span class="n">B</span>
<span class="o">/</span><span class="n">usr</span><span class="o">/</span><span class="n">lib</span><span class="o">/</span><span class="n">python3</span><span class="o">.</span><span class="mi">4</span><span class="o">/</span><span class="n">urllib</span><span class="o">/</span><span class="n">parse</span><span class="o">.</span><span class="n">py</span><span class="p">:</span><span class="mi">476</span><span class="p">:</span> <span class="n">size</span><span class="o">=</span><span class="mf">71.8</span> <span class="n">KiB</span> <span class="p">(</span><span class="o">+</span><span class="mf">71.8</span> <span class="n">KiB</span><span class="p">),</span> <span class="n">count</span><span class="o">=</span><span class="mi">969</span> <span class="p">(</span><span class="o">+</span><span class="mi">969</span><span class="p">),</span> <span class="n">average</span><span class="o">=</span><span class="mi">76</span> <span class="n">B</span>
<span class="o">/</span><span class="n">usr</span><span class="o">/</span><span class="n">lib</span><span class="o">/</span><span class="n">python3</span><span class="o">.</span><span class="mi">4</span><span class="o">/</span><span class="n">contextlib</span><span class="o">.</span><span class="n">py</span><span class="p">:</span><span class="mi">38</span><span class="p">:</span> <span class="n">size</span><span class="o">=</span><span class="mf">67.2</span> <span class="n">KiB</span> <span class="p">(</span><span class="o">+</span><span class="mf">67.2</span> <span class="n">KiB</span><span class="p">),</span> <span class="n">count</span><span class="o">=</span><span class="mi">126</span> <span class="p">(</span><span class="o">+</span><span class="mi">126</span><span class="p">),</span> <span class="n">average</span><span class="o">=</span><span class="mi">546</span> <span class="n">B</span>
</pre></div>
</div>
<p>We can see that Python has loaded <tt class="docutils literal"><span class="pre">8.2</span> <span class="pre">MiB</span></tt> of module data (bytecode and
constants), and that this is <tt class="docutils literal"><span class="pre">4.4</span> <span class="pre">MiB</span></tt> more than had been loaded before the
tests, when the previous snapshot was taken. Similarly, the <a class="reference internal" href="linecache.html#module-linecache" title="linecache: This module provides random access to individual lines from text files."><tt class="xref py py-mod docutils literal"><span class="pre">linecache</span></tt></a>
module has cached <tt class="docutils literal"><span class="pre">940</span> <span class="pre">KiB</span></tt> of Python source code to format tracebacks, all
of it since the previous snapshot.</p>
<p>If the system has little free memory, snapshots can be written on disk using
the <a class="reference internal" href="#tracemalloc.Snapshot.dump" title="tracemalloc.Snapshot.dump"><tt class="xref py py-meth docutils literal"><span class="pre">Snapshot.dump()</span></tt></a> method to analyze the snapshot offline. Then use the
<a class="reference internal" href="#tracemalloc.Snapshot.load" title="tracemalloc.Snapshot.load"><tt class="xref py py-meth docutils literal"><span class="pre">Snapshot.load()</span></tt></a> method reload the snapshot.</p>
</div>
<div class="section" id="get-the-traceback-of-a-memory-block">
<h3>27.7.1.3. Get the traceback of a memory block<a class="headerlink" href="#get-the-traceback-of-a-memory-block" title="Permalink to this headline">¶</a></h3>
<p>Code to display the traceback of the biggest memory block:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">tracemalloc</span>

<span class="c"># Store 25 frames</span>
<span class="n">tracemalloc</span><span class="o">.</span><span class="n">start</span><span class="p">(</span><span class="mi">25</span><span class="p">)</span>

<span class="c"># ... run your application ...</span>

<span class="n">snapshot</span> <span class="o">=</span> <span class="n">tracemalloc</span><span class="o">.</span><span class="n">take_snapshot</span><span class="p">()</span>
<span class="n">top_stats</span> <span class="o">=</span> <span class="n">snapshot</span><span class="o">.</span><span class="n">statistics</span><span class="p">(</span><span class="s">&#39;traceback&#39;</span><span class="p">)</span>

<span class="c"># pick the biggest memory block</span>
<span class="n">stat</span> <span class="o">=</span> <span class="n">top_stats</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
<span class="nb">print</span><span class="p">(</span><span class="s">&quot;%s memory blocks: %.1f KiB&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">stat</span><span class="o">.</span><span class="n">count</span><span class="p">,</span> <span class="n">stat</span><span class="o">.</span><span class="n">size</span> <span class="o">/</span> <span class="mi">1024</span><span class="p">))</span>
<span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">stat</span><span class="o">.</span><span class="n">traceback</span><span class="o">.</span><span class="n">format</span><span class="p">():</span>
    <span class="nb">print</span><span class="p">(</span><span class="n">line</span><span class="p">)</span>
</pre></div>
</div>
<p>Example of output of the Python test suite (traceback limited to 25 frames):</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="mi">903</span> <span class="n">memory</span> <span class="n">blocks</span><span class="p">:</span> <span class="mf">870.1</span> <span class="n">KiB</span>
  <span class="n">File</span> <span class="s">&quot;&lt;frozen importlib._bootstrap&gt;&quot;</span><span class="p">,</span> <span class="n">line</span> <span class="mi">716</span>
  <span class="n">File</span> <span class="s">&quot;&lt;frozen importlib._bootstrap&gt;&quot;</span><span class="p">,</span> <span class="n">line</span> <span class="mi">1036</span>
  <span class="n">File</span> <span class="s">&quot;&lt;frozen importlib._bootstrap&gt;&quot;</span><span class="p">,</span> <span class="n">line</span> <span class="mi">934</span>
  <span class="n">File</span> <span class="s">&quot;&lt;frozen importlib._bootstrap&gt;&quot;</span><span class="p">,</span> <span class="n">line</span> <span class="mi">1068</span>
  <span class="n">File</span> <span class="s">&quot;&lt;frozen importlib._bootstrap&gt;&quot;</span><span class="p">,</span> <span class="n">line</span> <span class="mi">619</span>
  <span class="n">File</span> <span class="s">&quot;&lt;frozen importlib._bootstrap&gt;&quot;</span><span class="p">,</span> <span class="n">line</span> <span class="mi">1581</span>
  <span class="n">File</span> <span class="s">&quot;&lt;frozen importlib._bootstrap&gt;&quot;</span><span class="p">,</span> <span class="n">line</span> <span class="mi">1614</span>
  <span class="n">File</span> <span class="s">&quot;/usr/lib/python3.4/doctest.py&quot;</span><span class="p">,</span> <span class="n">line</span> <span class="mi">101</span>
    <span class="kn">import</span> <span class="nn">pdb</span>
  <span class="n">File</span> <span class="s">&quot;&lt;frozen importlib._bootstrap&gt;&quot;</span><span class="p">,</span> <span class="n">line</span> <span class="mi">284</span>
  <span class="n">File</span> <span class="s">&quot;&lt;frozen importlib._bootstrap&gt;&quot;</span><span class="p">,</span> <span class="n">line</span> <span class="mi">938</span>
  <span class="n">File</span> <span class="s">&quot;&lt;frozen importlib._bootstrap&gt;&quot;</span><span class="p">,</span> <span class="n">line</span> <span class="mi">1068</span>
  <span class="n">File</span> <span class="s">&quot;&lt;frozen importlib._bootstrap&gt;&quot;</span><span class="p">,</span> <span class="n">line</span> <span class="mi">619</span>
  <span class="n">File</span> <span class="s">&quot;&lt;frozen importlib._bootstrap&gt;&quot;</span><span class="p">,</span> <span class="n">line</span> <span class="mi">1581</span>
  <span class="n">File</span> <span class="s">&quot;&lt;frozen importlib._bootstrap&gt;&quot;</span><span class="p">,</span> <span class="n">line</span> <span class="mi">1614</span>
  <span class="n">File</span> <span class="s">&quot;/usr/lib/python3.4/test/support/__init__.py&quot;</span><span class="p">,</span> <span class="n">line</span> <span class="mi">1728</span>
    <span class="kn">import</span> <span class="nn">doctest</span>
  <span class="n">File</span> <span class="s">&quot;/usr/lib/python3.4/test/test_pickletools.py&quot;</span><span class="p">,</span> <span class="n">line</span> <span class="mi">21</span>
    <span class="n">support</span><span class="o">.</span><span class="n">run_doctest</span><span class="p">(</span><span class="n">pickletools</span><span class="p">)</span>
  <span class="n">File</span> <span class="s">&quot;/usr/lib/python3.4/test/regrtest.py&quot;</span><span class="p">,</span> <span class="n">line</span> <span class="mi">1276</span>
    <span class="n">test_runner</span><span class="p">()</span>
  <span class="n">File</span> <span class="s">&quot;/usr/lib/python3.4/test/regrtest.py&quot;</span><span class="p">,</span> <span class="n">line</span> <span class="mi">976</span>
    <span class="n">display_failure</span><span class="o">=</span><span class="ow">not</span> <span class="n">verbose</span><span class="p">)</span>
  <span class="n">File</span> <span class="s">&quot;/usr/lib/python3.4/test/regrtest.py&quot;</span><span class="p">,</span> <span class="n">line</span> <span class="mi">761</span>
    <span class="n">match_tests</span><span class="o">=</span><span class="n">ns</span><span class="o">.</span><span class="n">match_tests</span><span class="p">)</span>
  <span class="n">File</span> <span class="s">&quot;/usr/lib/python3.4/test/regrtest.py&quot;</span><span class="p">,</span> <span class="n">line</span> <span class="mi">1563</span>
    <span class="n">main</span><span class="p">()</span>
  <span class="n">File</span> <span class="s">&quot;/usr/lib/python3.4/test/__main__.py&quot;</span><span class="p">,</span> <span class="n">line</span> <span class="mi">3</span>
    <span class="n">regrtest</span><span class="o">.</span><span class="n">main_in_temp_cwd</span><span class="p">()</span>
  <span class="n">File</span> <span class="s">&quot;/usr/lib/python3.4/runpy.py&quot;</span><span class="p">,</span> <span class="n">line</span> <span class="mi">73</span>
    <span class="n">exec</span><span class="p">(</span><span class="n">code</span><span class="p">,</span> <span class="n">run_globals</span><span class="p">)</span>
  <span class="n">File</span> <span class="s">&quot;/usr/lib/python3.4/runpy.py&quot;</span><span class="p">,</span> <span class="n">line</span> <span class="mi">160</span>
    <span class="s">&quot;__main__&quot;</span><span class="p">,</span> <span class="n">fname</span><span class="p">,</span> <span class="n">loader</span><span class="p">,</span> <span class="n">pkg_name</span><span class="p">)</span>
</pre></div>
</div>
<p>We can see that the most memory was allocated in the <a class="reference internal" href="importlib.html#module-importlib" title="importlib: The implementation of the import machinery."><tt class="xref py py-mod docutils literal"><span class="pre">importlib</span></tt></a> module to
load data (bytecode and constants) from modules: <tt class="docutils literal"><span class="pre">870</span> <span class="pre">KiB</span></tt>. The traceback is
where the <a class="reference internal" href="importlib.html#module-importlib" title="importlib: The implementation of the import machinery."><tt class="xref py py-mod docutils literal"><span class="pre">importlib</span></tt></a> loaded data most recently: on the <tt class="docutils literal"><span class="pre">import</span> <span class="pre">pdb</span></tt>
line of the <a class="reference internal" href="doctest.html#module-doctest" title="doctest: Test pieces of code within docstrings."><tt class="xref py py-mod docutils literal"><span class="pre">doctest</span></tt></a> module. The traceback may change if a new module is
loaded.</p>
</div>
<div class="section" id="pretty-top">
<h3>27.7.1.4. Pretty top<a class="headerlink" href="#pretty-top" title="Permalink to this headline">¶</a></h3>
<p>Code to display the 10 lines allocating the most memory with a pretty output,
ignoring <tt class="docutils literal"><span class="pre">&lt;frozen</span> <span class="pre">importlib._bootstrap&gt;</span></tt> and <tt class="docutils literal"><span class="pre">&lt;unknown&gt;</span></tt> files:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">linecache</span>
<span class="kn">import</span> <span class="nn">os</span>
<span class="kn">import</span> <span class="nn">tracemalloc</span>

<span class="k">def</span> <span class="nf">display_top</span><span class="p">(</span><span class="n">snapshot</span><span class="p">,</span> <span class="n">group_by</span><span class="o">=</span><span class="s">&#39;lineno&#39;</span><span class="p">,</span> <span class="n">limit</span><span class="o">=</span><span class="mi">10</span><span class="p">):</span>
    <span class="n">snapshot</span> <span class="o">=</span> <span class="n">snapshot</span><span class="o">.</span><span class="n">filter_traces</span><span class="p">((</span>
        <span class="n">tracemalloc</span><span class="o">.</span><span class="n">Filter</span><span class="p">(</span><span class="k">False</span><span class="p">,</span> <span class="s">&quot;&lt;frozen importlib._bootstrap&gt;&quot;</span><span class="p">),</span>
        <span class="n">tracemalloc</span><span class="o">.</span><span class="n">Filter</span><span class="p">(</span><span class="k">False</span><span class="p">,</span> <span class="s">&quot;&lt;unknown&gt;&quot;</span><span class="p">),</span>
    <span class="p">))</span>
    <span class="n">top_stats</span> <span class="o">=</span> <span class="n">snapshot</span><span class="o">.</span><span class="n">statistics</span><span class="p">(</span><span class="n">group_by</span><span class="p">)</span>

    <span class="nb">print</span><span class="p">(</span><span class="s">&quot;Top %s lines&quot;</span> <span class="o">%</span> <span class="n">limit</span><span class="p">)</span>
    <span class="k">for</span> <span class="n">index</span><span class="p">,</span> <span class="n">stat</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">top_stats</span><span class="p">[:</span><span class="n">limit</span><span class="p">],</span> <span class="mi">1</span><span class="p">):</span>
        <span class="n">frame</span> <span class="o">=</span> <span class="n">stat</span><span class="o">.</span><span class="n">traceback</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
        <span class="c"># replace &quot;/path/to/module/file.py&quot; with &quot;module/file.py&quot;</span>
        <span class="n">filename</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">sep</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">frame</span><span class="o">.</span><span class="n">filename</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">sep</span><span class="p">)[</span><span class="o">-</span><span class="mi">2</span><span class="p">:])</span>
        <span class="nb">print</span><span class="p">(</span><span class="s">&quot;#%s: %s:%s: %.1f KiB&quot;</span>
              <span class="o">%</span> <span class="p">(</span><span class="n">index</span><span class="p">,</span> <span class="n">filename</span><span class="p">,</span> <span class="n">frame</span><span class="o">.</span><span class="n">lineno</span><span class="p">,</span> <span class="n">stat</span><span class="o">.</span><span class="n">size</span> <span class="o">/</span> <span class="mi">1024</span><span class="p">))</span>
        <span class="n">line</span> <span class="o">=</span> <span class="n">linecache</span><span class="o">.</span><span class="n">getline</span><span class="p">(</span><span class="n">frame</span><span class="o">.</span><span class="n">filename</span><span class="p">,</span> <span class="n">frame</span><span class="o">.</span><span class="n">lineno</span><span class="p">)</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span>
        <span class="k">if</span> <span class="n">line</span><span class="p">:</span>
            <span class="nb">print</span><span class="p">(</span><span class="s">&#39;    %s&#39;</span> <span class="o">%</span> <span class="n">line</span><span class="p">)</span>

    <span class="n">other</span> <span class="o">=</span> <span class="n">top_stats</span><span class="p">[</span><span class="n">limit</span><span class="p">:]</span>
    <span class="k">if</span> <span class="n">other</span><span class="p">:</span>
        <span class="n">size</span> <span class="o">=</span> <span class="nb">sum</span><span class="p">(</span><span class="n">stat</span><span class="o">.</span><span class="n">size</span> <span class="k">for</span> <span class="n">stat</span> <span class="ow">in</span> <span class="n">other</span><span class="p">)</span>
        <span class="nb">print</span><span class="p">(</span><span class="s">&quot;%s other: %.1f KiB&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">other</span><span class="p">),</span> <span class="n">size</span> <span class="o">/</span> <span class="mi">1024</span><span class="p">))</span>
    <span class="n">total</span> <span class="o">=</span> <span class="nb">sum</span><span class="p">(</span><span class="n">stat</span><span class="o">.</span><span class="n">size</span> <span class="k">for</span> <span class="n">stat</span> <span class="ow">in</span> <span class="n">top_stats</span><span class="p">)</span>
    <span class="nb">print</span><span class="p">(</span><span class="s">&quot;Total allocated size: %.1f KiB&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">total</span> <span class="o">/</span> <span class="mi">1024</span><span class="p">))</span>

<span class="n">tracemalloc</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>

<span class="c"># ... run your application ...</span>

<span class="n">snapshot</span> <span class="o">=</span> <span class="n">tracemalloc</span><span class="o">.</span><span class="n">take_snapshot</span><span class="p">()</span>
<span class="n">display_top</span><span class="p">(</span><span class="n">snapshot</span><span class="p">)</span>
</pre></div>
</div>
<p>Example of output of the Python test suite:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="n">Top</span> <span class="mi">10</span> <span class="n">lines</span>
<span class="c">#1: Lib/base64.py:414: 419.8 KiB</span>
    <span class="n">_b85chars2</span> <span class="o">=</span> <span class="p">[(</span><span class="n">a</span> <span class="o">+</span> <span class="n">b</span><span class="p">)</span> <span class="k">for</span> <span class="n">a</span> <span class="ow">in</span> <span class="n">_b85chars</span> <span class="k">for</span> <span class="n">b</span> <span class="ow">in</span> <span class="n">_b85chars</span><span class="p">]</span>
<span class="c">#2: Lib/base64.py:306: 419.8 KiB</span>
    <span class="n">_a85chars2</span> <span class="o">=</span> <span class="p">[(</span><span class="n">a</span> <span class="o">+</span> <span class="n">b</span><span class="p">)</span> <span class="k">for</span> <span class="n">a</span> <span class="ow">in</span> <span class="n">_a85chars</span> <span class="k">for</span> <span class="n">b</span> <span class="ow">in</span> <span class="n">_a85chars</span><span class="p">]</span>
<span class="c">#3: collections/__init__.py:368: 293.6 KiB</span>
    <span class="n">exec</span><span class="p">(</span><span class="n">class_definition</span><span class="p">,</span> <span class="n">namespace</span><span class="p">)</span>
<span class="c">#4: Lib/abc.py:133: 115.2 KiB</span>
    <span class="n">cls</span> <span class="o">=</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">__new__</span><span class="p">(</span><span class="n">mcls</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">bases</span><span class="p">,</span> <span class="n">namespace</span><span class="p">)</span>
<span class="c">#5: unittest/case.py:574: 103.1 KiB</span>
    <span class="n">testMethod</span><span class="p">()</span>
<span class="c">#6: Lib/linecache.py:127: 95.4 KiB</span>
    <span class="n">lines</span> <span class="o">=</span> <span class="n">fp</span><span class="o">.</span><span class="n">readlines</span><span class="p">()</span>
<span class="c">#7: urllib/parse.py:476: 71.8 KiB</span>
    <span class="k">for</span> <span class="n">a</span> <span class="ow">in</span> <span class="n">_hexdig</span> <span class="k">for</span> <span class="n">b</span> <span class="ow">in</span> <span class="n">_hexdig</span><span class="p">}</span>
<span class="c">#8: &lt;string&gt;:5: 62.0 KiB</span>
<span class="c">#9: Lib/_weakrefset.py:37: 60.0 KiB</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">data</span> <span class="o">=</span> <span class="nb">set</span><span class="p">()</span>
<span class="c">#10: Lib/base64.py:142: 59.8 KiB</span>
    <span class="n">_b32tab2</span> <span class="o">=</span> <span class="p">[</span><span class="n">a</span> <span class="o">+</span> <span class="n">b</span> <span class="k">for</span> <span class="n">a</span> <span class="ow">in</span> <span class="n">_b32tab</span> <span class="k">for</span> <span class="n">b</span> <span class="ow">in</span> <span class="n">_b32tab</span><span class="p">]</span>
<span class="mi">6220</span> <span class="n">other</span><span class="p">:</span> <span class="mf">3602.8</span> <span class="n">KiB</span>
<span class="n">Total</span> <span class="n">allocated</span> <span class="n">size</span><span class="p">:</span> <span class="mf">5303.1</span> <span class="n">KiB</span>
</pre></div>
</div>
<p>See <a class="reference internal" href="#tracemalloc.Snapshot.statistics" title="tracemalloc.Snapshot.statistics"><tt class="xref py py-meth docutils literal"><span class="pre">Snapshot.statistics()</span></tt></a> for more options.</p>
</div>
</div>
<div class="section" id="api">
<h2>27.7.2. API<a class="headerlink" href="#api" title="Permalink to this headline">¶</a></h2>
<div class="section" id="functions">
<h3>27.7.2.1. Functions<a class="headerlink" href="#functions" title="Permalink to this headline">¶</a></h3>
<dl class="function">
<dt id="tracemalloc.clear_traces">
<tt class="descclassname">tracemalloc.</tt><tt class="descname">clear_traces</tt><big>(</big><big>)</big><a class="headerlink" href="#tracemalloc.clear_traces" title="Permalink to this definition">¶</a></dt>
<dd><p>Clear traces of memory blocks allocated by Python.</p>
<p>See also <a class="reference internal" href="#tracemalloc.stop" title="tracemalloc.stop"><tt class="xref py py-func docutils literal"><span class="pre">stop()</span></tt></a>.</p>
</dd></dl>

<dl class="function">
<dt id="tracemalloc.get_object_traceback">
<tt class="descclassname">tracemalloc.</tt><tt class="descname">get_object_traceback</tt><big>(</big><em>obj</em><big>)</big><a class="headerlink" href="#tracemalloc.get_object_traceback" title="Permalink to this definition">¶</a></dt>
<dd><p>Get the traceback where the Python object <em>obj</em> was allocated.
Return a <a class="reference internal" href="#tracemalloc.Traceback" title="tracemalloc.Traceback"><tt class="xref py py-class docutils literal"><span class="pre">Traceback</span></tt></a> instance, or <tt class="docutils literal"><span class="pre">None</span></tt> if the <a class="reference internal" href="#module-tracemalloc" title="tracemalloc: Trace memory allocations."><tt class="xref py py-mod docutils literal"><span class="pre">tracemalloc</span></tt></a>
module is not tracing memory allocations or did not trace the allocation of
the object.</p>
<p>See also <a class="reference internal" href="gc.html#gc.get_referrers" title="gc.get_referrers"><tt class="xref py py-func docutils literal"><span class="pre">gc.get_referrers()</span></tt></a> and <a class="reference internal" href="sys.html#sys.getsizeof" title="sys.getsizeof"><tt class="xref py py-func docutils literal"><span class="pre">sys.getsizeof()</span></tt></a> functions.</p>
</dd></dl>

<dl class="function">
<dt id="tracemalloc.get_traceback_limit">
<tt class="descclassname">tracemalloc.</tt><tt class="descname">get_traceback_limit</tt><big>(</big><big>)</big><a class="headerlink" href="#tracemalloc.get_traceback_limit" title="Permalink to this definition">¶</a></dt>
<dd><p>Get the maximum number of frames stored in the traceback of a trace.</p>
<p>The <a class="reference internal" href="#module-tracemalloc" title="tracemalloc: Trace memory allocations."><tt class="xref py py-mod docutils literal"><span class="pre">tracemalloc</span></tt></a> module must be tracing memory allocations to
get the limit, otherwise an exception is raised.</p>
<p>The limit is set by the <a class="reference internal" href="#tracemalloc.start" title="tracemalloc.start"><tt class="xref py py-func docutils literal"><span class="pre">start()</span></tt></a> function.</p>
</dd></dl>

<dl class="function">
<dt id="tracemalloc.get_traced_memory">
<tt class="descclassname">tracemalloc.</tt><tt class="descname">get_traced_memory</tt><big>(</big><big>)</big><a class="headerlink" href="#tracemalloc.get_traced_memory" title="Permalink to this definition">¶</a></dt>
<dd><p>Get the current size and peak size of memory blocks traced by the
<a class="reference internal" href="#module-tracemalloc" title="tracemalloc: Trace memory allocations."><tt class="xref py py-mod docutils literal"><span class="pre">tracemalloc</span></tt></a> module as a tuple: <tt class="docutils literal"><span class="pre">(current:</span> <span class="pre">int,</span> <span class="pre">peak:</span> <span class="pre">int)</span></tt>.</p>
</dd></dl>

<dl class="function">
<dt id="tracemalloc.get_tracemalloc_memory">
<tt class="descclassname">tracemalloc.</tt><tt class="descname">get_tracemalloc_memory</tt><big>(</big><big>)</big><a class="headerlink" href="#tracemalloc.get_tracemalloc_memory" title="Permalink to this definition">¶</a></dt>
<dd><p>Get the memory usage in bytes of the <a class="reference internal" href="#module-tracemalloc" title="tracemalloc: Trace memory allocations."><tt class="xref py py-mod docutils literal"><span class="pre">tracemalloc</span></tt></a> module used to store
traces of memory blocks.
Return an <a class="reference internal" href="functions.html#int" title="int"><tt class="xref py py-class docutils literal"><span class="pre">int</span></tt></a>.</p>
</dd></dl>

<dl class="function">
<dt id="tracemalloc.is_tracing">
<tt class="descclassname">tracemalloc.</tt><tt class="descname">is_tracing</tt><big>(</big><big>)</big><a class="headerlink" href="#tracemalloc.is_tracing" title="Permalink to this definition">¶</a></dt>
<dd><p><tt class="docutils literal"><span class="pre">True</span></tt> if the <a class="reference internal" href="#module-tracemalloc" title="tracemalloc: Trace memory allocations."><tt class="xref py py-mod docutils literal"><span class="pre">tracemalloc</span></tt></a> module is tracing Python memory
allocations, <tt class="docutils literal"><span class="pre">False</span></tt> otherwise.</p>
<p>See also <a class="reference internal" href="#tracemalloc.start" title="tracemalloc.start"><tt class="xref py py-func docutils literal"><span class="pre">start()</span></tt></a> and <a class="reference internal" href="#tracemalloc.stop" title="tracemalloc.stop"><tt class="xref py py-func docutils literal"><span class="pre">stop()</span></tt></a> functions.</p>
</dd></dl>

<dl class="function">
<dt id="tracemalloc.start">
<tt class="descclassname">tracemalloc.</tt><tt class="descname">start</tt><big>(</big><em>nframe: int=1</em><big>)</big><a class="headerlink" href="#tracemalloc.start" title="Permalink to this definition">¶</a></dt>
<dd><p>Start tracing Python memory allocations: install hooks on Python memory
allocators. Collected tracebacks of traces will be limited to <em>nframe</em>
frames. By default, a trace of a memory block only stores the most recent
frame: the limit is <tt class="docutils literal"><span class="pre">1</span></tt>. <em>nframe</em> must be greater or equal to <tt class="docutils literal"><span class="pre">1</span></tt>.</p>
<p>Storing more than <tt class="docutils literal"><span class="pre">1</span></tt> frame is only useful to compute statistics grouped
by <tt class="docutils literal"><span class="pre">'traceback'</span></tt> or to compute cumulative statistics: see the
<a class="reference internal" href="#tracemalloc.Snapshot.compare_to" title="tracemalloc.Snapshot.compare_to"><tt class="xref py py-meth docutils literal"><span class="pre">Snapshot.compare_to()</span></tt></a> and <a class="reference internal" href="#tracemalloc.Snapshot.statistics" title="tracemalloc.Snapshot.statistics"><tt class="xref py py-meth docutils literal"><span class="pre">Snapshot.statistics()</span></tt></a> methods.</p>
<p>Storing more frames increases the memory and CPU overhead of the
<a class="reference internal" href="#module-tracemalloc" title="tracemalloc: Trace memory allocations."><tt class="xref py py-mod docutils literal"><span class="pre">tracemalloc</span></tt></a> module. Use the <a class="reference internal" href="#tracemalloc.get_tracemalloc_memory" title="tracemalloc.get_tracemalloc_memory"><tt class="xref py py-func docutils literal"><span class="pre">get_tracemalloc_memory()</span></tt></a> function
to measure how much memory is used by the <a class="reference internal" href="#module-tracemalloc" title="tracemalloc: Trace memory allocations."><tt class="xref py py-mod docutils literal"><span class="pre">tracemalloc</span></tt></a> module.</p>
<p>The <span class="target" id="index-2"></span><a class="reference internal" href="../using/cmdline.html#envvar-PYTHONTRACEMALLOC"><tt class="xref std std-envvar docutils literal"><span class="pre">PYTHONTRACEMALLOC</span></tt></a> environment variable
(<tt class="docutils literal"><span class="pre">PYTHONTRACEMALLOC=NFRAME</span></tt>) and the <a class="reference internal" href="../using/cmdline.html#cmdoption-X"><em class="xref std std-option">-X</em></a> <tt class="docutils literal"><span class="pre">tracemalloc=NFRAME</span></tt>
command line option can be used to start tracing at startup.</p>
<p>See also <a class="reference internal" href="#tracemalloc.stop" title="tracemalloc.stop"><tt class="xref py py-func docutils literal"><span class="pre">stop()</span></tt></a>, <a class="reference internal" href="#tracemalloc.is_tracing" title="tracemalloc.is_tracing"><tt class="xref py py-func docutils literal"><span class="pre">is_tracing()</span></tt></a> and <a class="reference internal" href="#tracemalloc.get_traceback_limit" title="tracemalloc.get_traceback_limit"><tt class="xref py py-func docutils literal"><span class="pre">get_traceback_limit()</span></tt></a>
functions.</p>
</dd></dl>

<dl class="function">
<dt id="tracemalloc.stop">
<tt class="descclassname">tracemalloc.</tt><tt class="descname">stop</tt><big>(</big><big>)</big><a class="headerlink" href="#tracemalloc.stop" title="Permalink to this definition">¶</a></dt>
<dd><p>Stop tracing Python memory allocations: uninstall hooks on Python memory
allocators. Also clears all previously collected traces of memory blocks
allocated by Python.</p>
<p>Call <a class="reference internal" href="#tracemalloc.take_snapshot" title="tracemalloc.take_snapshot"><tt class="xref py py-func docutils literal"><span class="pre">take_snapshot()</span></tt></a> function to take a snapshot of traces before
clearing them.</p>
<p>See also <a class="reference internal" href="#tracemalloc.start" title="tracemalloc.start"><tt class="xref py py-func docutils literal"><span class="pre">start()</span></tt></a>, <a class="reference internal" href="#tracemalloc.is_tracing" title="tracemalloc.is_tracing"><tt class="xref py py-func docutils literal"><span class="pre">is_tracing()</span></tt></a> and <a class="reference internal" href="#tracemalloc.clear_traces" title="tracemalloc.clear_traces"><tt class="xref py py-func docutils literal"><span class="pre">clear_traces()</span></tt></a>
functions.</p>
</dd></dl>

<dl class="function">
<dt id="tracemalloc.take_snapshot">
<tt class="descclassname">tracemalloc.</tt><tt class="descname">take_snapshot</tt><big>(</big><big>)</big><a class="headerlink" href="#tracemalloc.take_snapshot" title="Permalink to this definition">¶</a></dt>
<dd><p>Take a snapshot of traces of memory blocks allocated by Python. Return a new
<a class="reference internal" href="#tracemalloc.Snapshot" title="tracemalloc.Snapshot"><tt class="xref py py-class docutils literal"><span class="pre">Snapshot</span></tt></a> instance.</p>
<p>The snapshot does not include memory blocks allocated before the
<a class="reference internal" href="#module-tracemalloc" title="tracemalloc: Trace memory allocations."><tt class="xref py py-mod docutils literal"><span class="pre">tracemalloc</span></tt></a> module started to trace memory allocations.</p>
<p>Tracebacks of traces are limited to <a class="reference internal" href="#tracemalloc.get_traceback_limit" title="tracemalloc.get_traceback_limit"><tt class="xref py py-func docutils literal"><span class="pre">get_traceback_limit()</span></tt></a> frames. Use
the <em>nframe</em> parameter of the <a class="reference internal" href="#tracemalloc.start" title="tracemalloc.start"><tt class="xref py py-func docutils literal"><span class="pre">start()</span></tt></a> function to store more frames.</p>
<p>The <a class="reference internal" href="#module-tracemalloc" title="tracemalloc: Trace memory allocations."><tt class="xref py py-mod docutils literal"><span class="pre">tracemalloc</span></tt></a> module must be tracing memory allocations to take a
snapshot, see the <a class="reference internal" href="#tracemalloc.start" title="tracemalloc.start"><tt class="xref py py-func docutils literal"><span class="pre">start()</span></tt></a> function.</p>
<p>See also the <a class="reference internal" href="#tracemalloc.get_object_traceback" title="tracemalloc.get_object_traceback"><tt class="xref py py-func docutils literal"><span class="pre">get_object_traceback()</span></tt></a> function.</p>
</dd></dl>

</div>
<div class="section" id="filter">
<h3>27.7.2.2. Filter<a class="headerlink" href="#filter" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="tracemalloc.Filter">
<em class="property">class </em><tt class="descclassname">tracemalloc.</tt><tt class="descname">Filter</tt><big>(</big><em>inclusive: bool</em>, <em>filename_pattern: str</em>, <em>lineno: int=None</em>, <em>all_frames: bool=False</em><big>)</big><a class="headerlink" href="#tracemalloc.Filter" title="Permalink to this definition">¶</a></dt>
<dd><p>Filter on traces of memory blocks.</p>
<p>See the <a class="reference internal" href="fnmatch.html#fnmatch.fnmatch" title="fnmatch.fnmatch"><tt class="xref py py-func docutils literal"><span class="pre">fnmatch.fnmatch()</span></tt></a> function for the syntax of
<em>filename_pattern</em>. The <tt class="docutils literal"><span class="pre">'.pyc'</span></tt> and <tt class="docutils literal"><span class="pre">'.pyo'</span></tt> file extensions are
replaced with <tt class="docutils literal"><span class="pre">'.py'</span></tt>.</p>
<p>Examples:</p>
<ul class="simple">
<li><tt class="docutils literal"><span class="pre">Filter(True,</span> <span class="pre">subprocess.__file__)</span></tt> only includes traces of the
<a class="reference internal" href="subprocess.html#module-subprocess" title="subprocess: Subprocess management."><tt class="xref py py-mod docutils literal"><span class="pre">subprocess</span></tt></a> module</li>
<li><tt class="docutils literal"><span class="pre">Filter(False,</span> <span class="pre">tracemalloc.__file__)</span></tt> excludes traces of the
<a class="reference internal" href="#module-tracemalloc" title="tracemalloc: Trace memory allocations."><tt class="xref py py-mod docutils literal"><span class="pre">tracemalloc</span></tt></a> module</li>
<li><tt class="docutils literal"><span class="pre">Filter(False,</span> <span class="pre">&quot;&lt;unknown&gt;&quot;)</span></tt> excludes empty tracebacks</li>
</ul>
<dl class="attribute">
<dt id="tracemalloc.Filter.inclusive">
<tt class="descname">inclusive</tt><a class="headerlink" href="#tracemalloc.Filter.inclusive" title="Permalink to this definition">¶</a></dt>
<dd><p>If <em>inclusive</em> is <tt class="docutils literal"><span class="pre">True</span></tt> (include), only trace memory blocks allocated
in a file with a name matching <a class="reference internal" href="#tracemalloc.Filter.filename_pattern" title="tracemalloc.Filter.filename_pattern"><tt class="xref py py-attr docutils literal"><span class="pre">filename_pattern</span></tt></a> at line number
<a class="reference internal" href="#tracemalloc.Filter.lineno" title="tracemalloc.Filter.lineno"><tt class="xref py py-attr docutils literal"><span class="pre">lineno</span></tt></a>.</p>
<p>If <em>inclusive</em> is <tt class="docutils literal"><span class="pre">False</span></tt> (exclude), ignore memory blocks allocated in
a file with a name matching <a class="reference internal" href="#tracemalloc.Filter.filename_pattern" title="tracemalloc.Filter.filename_pattern"><tt class="xref py py-attr docutils literal"><span class="pre">filename_pattern</span></tt></a> at line number
<a class="reference internal" href="#tracemalloc.Filter.lineno" title="tracemalloc.Filter.lineno"><tt class="xref py py-attr docutils literal"><span class="pre">lineno</span></tt></a>.</p>
</dd></dl>

<dl class="attribute">
<dt id="tracemalloc.Filter.lineno">
<tt class="descname">lineno</tt><a class="headerlink" href="#tracemalloc.Filter.lineno" title="Permalink to this definition">¶</a></dt>
<dd><p>Line number (<tt class="docutils literal"><span class="pre">int</span></tt>) of the filter. If <em>lineno</em> is <tt class="docutils literal"><span class="pre">None</span></tt>, the filter
matches any line number.</p>
</dd></dl>

<dl class="attribute">
<dt id="tracemalloc.Filter.filename_pattern">
<tt class="descname">filename_pattern</tt><a class="headerlink" href="#tracemalloc.Filter.filename_pattern" title="Permalink to this definition">¶</a></dt>
<dd><p>Filename pattern of the filter (<tt class="docutils literal"><span class="pre">str</span></tt>).</p>
</dd></dl>

<dl class="attribute">
<dt id="tracemalloc.Filter.all_frames">
<tt class="descname">all_frames</tt><a class="headerlink" href="#tracemalloc.Filter.all_frames" title="Permalink to this definition">¶</a></dt>
<dd><p>If <em>all_frames</em> is <tt class="docutils literal"><span class="pre">True</span></tt>, all frames of the traceback are checked. If
<em>all_frames</em> is <tt class="docutils literal"><span class="pre">False</span></tt>, only the most recent frame is checked.</p>
<p>This attribute has no effect if the traceback limit is <tt class="docutils literal"><span class="pre">1</span></tt>.  See the
<a class="reference internal" href="#tracemalloc.get_traceback_limit" title="tracemalloc.get_traceback_limit"><tt class="xref py py-func docutils literal"><span class="pre">get_traceback_limit()</span></tt></a> function and <a class="reference internal" href="#tracemalloc.Snapshot.traceback_limit" title="tracemalloc.Snapshot.traceback_limit"><tt class="xref py py-attr docutils literal"><span class="pre">Snapshot.traceback_limit</span></tt></a>
attribute.</p>
</dd></dl>

</dd></dl>

</div>
<div class="section" id="frame">
<h3>27.7.2.3. Frame<a class="headerlink" href="#frame" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="tracemalloc.Frame">
<em class="property">class </em><tt class="descclassname">tracemalloc.</tt><tt class="descname">Frame</tt><a class="headerlink" href="#tracemalloc.Frame" title="Permalink to this definition">¶</a></dt>
<dd><p>Frame of a traceback.</p>
<p>The <a class="reference internal" href="#tracemalloc.Traceback" title="tracemalloc.Traceback"><tt class="xref py py-class docutils literal"><span class="pre">Traceback</span></tt></a> class is a sequence of <a class="reference internal" href="#tracemalloc.Frame" title="tracemalloc.Frame"><tt class="xref py py-class docutils literal"><span class="pre">Frame</span></tt></a> instances.</p>
<dl class="attribute">
<dt id="tracemalloc.Frame.filename">
<tt class="descname">filename</tt><a class="headerlink" href="#tracemalloc.Frame.filename" title="Permalink to this definition">¶</a></dt>
<dd><p>Filename (<tt class="docutils literal"><span class="pre">str</span></tt>).</p>
</dd></dl>

<dl class="attribute">
<dt id="tracemalloc.Frame.lineno">
<tt class="descname">lineno</tt><a class="headerlink" href="#tracemalloc.Frame.lineno" title="Permalink to this definition">¶</a></dt>
<dd><p>Line number (<tt class="docutils literal"><span class="pre">int</span></tt>).</p>
</dd></dl>

</dd></dl>

</div>
<div class="section" id="snapshot">
<h3>27.7.2.4. Snapshot<a class="headerlink" href="#snapshot" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="tracemalloc.Snapshot">
<em class="property">class </em><tt class="descclassname">tracemalloc.</tt><tt class="descname">Snapshot</tt><a class="headerlink" href="#tracemalloc.Snapshot" title="Permalink to this definition">¶</a></dt>
<dd><p>Snapshot of traces of memory blocks allocated by Python.</p>
<p>The <a class="reference internal" href="#tracemalloc.take_snapshot" title="tracemalloc.take_snapshot"><tt class="xref py py-func docutils literal"><span class="pre">take_snapshot()</span></tt></a> function creates a snapshot instance.</p>
<dl class="method">
<dt id="tracemalloc.Snapshot.compare_to">
<tt class="descname">compare_to</tt><big>(</big><em>old_snapshot: Snapshot</em>, <em>group_by: str</em>, <em>cumulative: bool=False</em><big>)</big><a class="headerlink" href="#tracemalloc.Snapshot.compare_to" title="Permalink to this definition">¶</a></dt>
<dd><p>Compute the differences with an old snapshot. Get statistics as a sorted
list of <a class="reference internal" href="#tracemalloc.StatisticDiff" title="tracemalloc.StatisticDiff"><tt class="xref py py-class docutils literal"><span class="pre">StatisticDiff</span></tt></a> instances grouped by <em>group_by</em>.</p>
<p>See the <a class="reference internal" href="#tracemalloc.Snapshot.statistics" title="tracemalloc.Snapshot.statistics"><tt class="xref py py-meth docutils literal"><span class="pre">Snapshot.statistics()</span></tt></a> method for <em>group_by</em> and <em>cumulative</em>
parameters.</p>
<p>The result is sorted from the biggest to the smallest by: absolute value
of <a class="reference internal" href="#tracemalloc.StatisticDiff.size_diff" title="tracemalloc.StatisticDiff.size_diff"><tt class="xref py py-attr docutils literal"><span class="pre">StatisticDiff.size_diff</span></tt></a>, <a class="reference internal" href="#tracemalloc.StatisticDiff.size" title="tracemalloc.StatisticDiff.size"><tt class="xref py py-attr docutils literal"><span class="pre">StatisticDiff.size</span></tt></a>, absolute
value of <a class="reference internal" href="#tracemalloc.StatisticDiff.count_diff" title="tracemalloc.StatisticDiff.count_diff"><tt class="xref py py-attr docutils literal"><span class="pre">StatisticDiff.count_diff</span></tt></a>, <a class="reference internal" href="#tracemalloc.Statistic.count" title="tracemalloc.Statistic.count"><tt class="xref py py-attr docutils literal"><span class="pre">Statistic.count</span></tt></a> and
then by <a class="reference internal" href="#tracemalloc.StatisticDiff.traceback" title="tracemalloc.StatisticDiff.traceback"><tt class="xref py py-attr docutils literal"><span class="pre">StatisticDiff.traceback</span></tt></a>.</p>
</dd></dl>

<dl class="method">
<dt id="tracemalloc.Snapshot.dump">
<tt class="descname">dump</tt><big>(</big><em>filename</em><big>)</big><a class="headerlink" href="#tracemalloc.Snapshot.dump" title="Permalink to this definition">¶</a></dt>
<dd><p>Write the snapshot into a file.</p>
<p>Use <a class="reference internal" href="#tracemalloc.Snapshot.load" title="tracemalloc.Snapshot.load"><tt class="xref py py-meth docutils literal"><span class="pre">load()</span></tt></a> to reload the snapshot.</p>
</dd></dl>

<dl class="method">
<dt id="tracemalloc.Snapshot.filter_traces">
<tt class="descname">filter_traces</tt><big>(</big><em>filters</em><big>)</big><a class="headerlink" href="#tracemalloc.Snapshot.filter_traces" title="Permalink to this definition">¶</a></dt>
<dd><p>Create a new <a class="reference internal" href="#tracemalloc.Snapshot" title="tracemalloc.Snapshot"><tt class="xref py py-class docutils literal"><span class="pre">Snapshot</span></tt></a> instance with a filtered <a class="reference internal" href="#tracemalloc.Snapshot.traces" title="tracemalloc.Snapshot.traces"><tt class="xref py py-attr docutils literal"><span class="pre">traces</span></tt></a>
sequence, <em>filters</em> is a list of <a class="reference internal" href="#tracemalloc.Filter" title="tracemalloc.Filter"><tt class="xref py py-class docutils literal"><span class="pre">Filter</span></tt></a> instances.  If <em>filters</em>
is an empty list, return a new <a class="reference internal" href="#tracemalloc.Snapshot" title="tracemalloc.Snapshot"><tt class="xref py py-class docutils literal"><span class="pre">Snapshot</span></tt></a> instance with a copy of
the traces.</p>
<p>All inclusive filters are applied at once, a trace is ignored if no
inclusive filters match it. A trace is ignored if at least one exclusive
filter matchs it.</p>
</dd></dl>

<dl class="classmethod">
<dt id="tracemalloc.Snapshot.load">
<em class="property">classmethod </em><tt class="descname">load</tt><big>(</big><em>filename</em><big>)</big><a class="headerlink" href="#tracemalloc.Snapshot.load" title="Permalink to this definition">¶</a></dt>
<dd><p>Load a snapshot from a file.</p>
<p>See also <a class="reference internal" href="#tracemalloc.Snapshot.dump" title="tracemalloc.Snapshot.dump"><tt class="xref py py-meth docutils literal"><span class="pre">dump()</span></tt></a>.</p>
</dd></dl>

<dl class="method">
<dt id="tracemalloc.Snapshot.statistics">
<tt class="descname">statistics</tt><big>(</big><em>group_by: str</em>, <em>cumulative: bool=False</em><big>)</big><a class="headerlink" href="#tracemalloc.Snapshot.statistics" title="Permalink to this definition">¶</a></dt>
<dd><p>Get statistics as a sorted list of <a class="reference internal" href="#tracemalloc.Statistic" title="tracemalloc.Statistic"><tt class="xref py py-class docutils literal"><span class="pre">Statistic</span></tt></a> instances grouped
by <em>group_by</em>:</p>
<table border="1" class="docutils">
<colgroup>
<col width="47%" />
<col width="53%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">group_by</th>
<th class="head">description</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td><tt class="docutils literal"><span class="pre">'filename'</span></tt></td>
<td>filename</td>
</tr>
<tr class="row-odd"><td><tt class="docutils literal"><span class="pre">'lineno'</span></tt></td>
<td>filename and line number</td>
</tr>
<tr class="row-even"><td><tt class="docutils literal"><span class="pre">'traceback'</span></tt></td>
<td>traceback</td>
</tr>
</tbody>
</table>
<p>If <em>cumulative</em> is <tt class="docutils literal"><span class="pre">True</span></tt>, cumulate size and count of memory blocks of
all frames of the traceback of a trace, not only the most recent frame.
The cumulative mode can only be used with <em>group_by</em> equals to
<tt class="docutils literal"><span class="pre">'filename'</span></tt> and <tt class="docutils literal"><span class="pre">'lineno'</span></tt>.</p>
<p>The result is sorted from the biggest to the smallest by:
<a class="reference internal" href="#tracemalloc.Statistic.size" title="tracemalloc.Statistic.size"><tt class="xref py py-attr docutils literal"><span class="pre">Statistic.size</span></tt></a>, <a class="reference internal" href="#tracemalloc.Statistic.count" title="tracemalloc.Statistic.count"><tt class="xref py py-attr docutils literal"><span class="pre">Statistic.count</span></tt></a> and then by
<a class="reference internal" href="#tracemalloc.Statistic.traceback" title="tracemalloc.Statistic.traceback"><tt class="xref py py-attr docutils literal"><span class="pre">Statistic.traceback</span></tt></a>.</p>
</dd></dl>

<dl class="attribute">
<dt id="tracemalloc.Snapshot.traceback_limit">
<tt class="descname">traceback_limit</tt><a class="headerlink" href="#tracemalloc.Snapshot.traceback_limit" title="Permalink to this definition">¶</a></dt>
<dd><p>Maximum number of frames stored in the traceback of <a class="reference internal" href="#tracemalloc.Snapshot.traces" title="tracemalloc.Snapshot.traces"><tt class="xref py py-attr docutils literal"><span class="pre">traces</span></tt></a>:
result of the <a class="reference internal" href="#tracemalloc.get_traceback_limit" title="tracemalloc.get_traceback_limit"><tt class="xref py py-func docutils literal"><span class="pre">get_traceback_limit()</span></tt></a> when the snapshot was taken.</p>
</dd></dl>

<dl class="attribute">
<dt id="tracemalloc.Snapshot.traces">
<tt class="descname">traces</tt><a class="headerlink" href="#tracemalloc.Snapshot.traces" title="Permalink to this definition">¶</a></dt>
<dd><p>Traces of all memory blocks allocated by Python: sequence of
<a class="reference internal" href="#tracemalloc.Trace" title="tracemalloc.Trace"><tt class="xref py py-class docutils literal"><span class="pre">Trace</span></tt></a> instances.</p>
<p>The sequence has an undefined order. Use the <a class="reference internal" href="#tracemalloc.Snapshot.statistics" title="tracemalloc.Snapshot.statistics"><tt class="xref py py-meth docutils literal"><span class="pre">Snapshot.statistics()</span></tt></a>
method to get a sorted list of statistics.</p>
</dd></dl>

</dd></dl>

</div>
<div class="section" id="statistic">
<h3>27.7.2.5. Statistic<a class="headerlink" href="#statistic" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="tracemalloc.Statistic">
<em class="property">class </em><tt class="descclassname">tracemalloc.</tt><tt class="descname">Statistic</tt><a class="headerlink" href="#tracemalloc.Statistic" title="Permalink to this definition">¶</a></dt>
<dd><p>Statistic on memory allocations.</p>
<p><a class="reference internal" href="#tracemalloc.Snapshot.statistics" title="tracemalloc.Snapshot.statistics"><tt class="xref py py-func docutils literal"><span class="pre">Snapshot.statistics()</span></tt></a> returns a list of <a class="reference internal" href="#tracemalloc.Statistic" title="tracemalloc.Statistic"><tt class="xref py py-class docutils literal"><span class="pre">Statistic</span></tt></a> instances.</p>
<p>See also the <a class="reference internal" href="#tracemalloc.StatisticDiff" title="tracemalloc.StatisticDiff"><tt class="xref py py-class docutils literal"><span class="pre">StatisticDiff</span></tt></a> class.</p>
<dl class="attribute">
<dt id="tracemalloc.Statistic.count">
<tt class="descname">count</tt><a class="headerlink" href="#tracemalloc.Statistic.count" title="Permalink to this definition">¶</a></dt>
<dd><p>Number of memory blocks (<tt class="docutils literal"><span class="pre">int</span></tt>).</p>
</dd></dl>

<dl class="attribute">
<dt id="tracemalloc.Statistic.size">
<tt class="descname">size</tt><a class="headerlink" href="#tracemalloc.Statistic.size" title="Permalink to this definition">¶</a></dt>
<dd><p>Total size of memory blocks in bytes (<tt class="docutils literal"><span class="pre">int</span></tt>).</p>
</dd></dl>

<dl class="attribute">
<dt id="tracemalloc.Statistic.traceback">
<tt class="descname">traceback</tt><a class="headerlink" href="#tracemalloc.Statistic.traceback" title="Permalink to this definition">¶</a></dt>
<dd><p>Traceback where the memory block was allocated, <a class="reference internal" href="#tracemalloc.Traceback" title="tracemalloc.Traceback"><tt class="xref py py-class docutils literal"><span class="pre">Traceback</span></tt></a>
instance.</p>
</dd></dl>

</dd></dl>

</div>
<div class="section" id="statisticdiff">
<h3>27.7.2.6. StatisticDiff<a class="headerlink" href="#statisticdiff" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="tracemalloc.StatisticDiff">
<em class="property">class </em><tt class="descclassname">tracemalloc.</tt><tt class="descname">StatisticDiff</tt><a class="headerlink" href="#tracemalloc.StatisticDiff" title="Permalink to this definition">¶</a></dt>
<dd><p>Statistic difference on memory allocations between an old and a new
<a class="reference internal" href="#tracemalloc.Snapshot" title="tracemalloc.Snapshot"><tt class="xref py py-class docutils literal"><span class="pre">Snapshot</span></tt></a> instance.</p>
<p><a class="reference internal" href="#tracemalloc.Snapshot.compare_to" title="tracemalloc.Snapshot.compare_to"><tt class="xref py py-func docutils literal"><span class="pre">Snapshot.compare_to()</span></tt></a> returns a list of <a class="reference internal" href="#tracemalloc.StatisticDiff" title="tracemalloc.StatisticDiff"><tt class="xref py py-class docutils literal"><span class="pre">StatisticDiff</span></tt></a>
instances. See also the <a class="reference internal" href="#tracemalloc.Statistic" title="tracemalloc.Statistic"><tt class="xref py py-class docutils literal"><span class="pre">Statistic</span></tt></a> class.</p>
<dl class="attribute">
<dt id="tracemalloc.StatisticDiff.count">
<tt class="descname">count</tt><a class="headerlink" href="#tracemalloc.StatisticDiff.count" title="Permalink to this definition">¶</a></dt>
<dd><p>Number of memory blocks in the new snapshot (<tt class="docutils literal"><span class="pre">int</span></tt>): <tt class="docutils literal"><span class="pre">0</span></tt> if
the memory blocks have been released in the new snapshot.</p>
</dd></dl>

<dl class="attribute">
<dt id="tracemalloc.StatisticDiff.count_diff">
<tt class="descname">count_diff</tt><a class="headerlink" href="#tracemalloc.StatisticDiff.count_diff" title="Permalink to this definition">¶</a></dt>
<dd><p>Difference of number of memory blocks between the old and the new
snapshots (<tt class="docutils literal"><span class="pre">int</span></tt>): <tt class="docutils literal"><span class="pre">0</span></tt> if the memory blocks have been allocated in
the new snapshot.</p>
</dd></dl>

<dl class="attribute">
<dt id="tracemalloc.StatisticDiff.size">
<tt class="descname">size</tt><a class="headerlink" href="#tracemalloc.StatisticDiff.size" title="Permalink to this definition">¶</a></dt>
<dd><p>Total size of memory blocks in bytes in the new snapshot (<tt class="docutils literal"><span class="pre">int</span></tt>):
<tt class="docutils literal"><span class="pre">0</span></tt> if the memory blocks have been released in the new snapshot.</p>
</dd></dl>

<dl class="attribute">
<dt id="tracemalloc.StatisticDiff.size_diff">
<tt class="descname">size_diff</tt><a class="headerlink" href="#tracemalloc.StatisticDiff.size_diff" title="Permalink to this definition">¶</a></dt>
<dd><p>Difference of total size of memory blocks in bytes between the old and
the new snapshots (<tt class="docutils literal"><span class="pre">int</span></tt>): <tt class="docutils literal"><span class="pre">0</span></tt> if the memory blocks have been
allocated in the new snapshot.</p>
</dd></dl>

<dl class="attribute">
<dt id="tracemalloc.StatisticDiff.traceback">
<tt class="descname">traceback</tt><a class="headerlink" href="#tracemalloc.StatisticDiff.traceback" title="Permalink to this definition">¶</a></dt>
<dd><p>Traceback where the memory blocks were allocated, <a class="reference internal" href="#tracemalloc.Traceback" title="tracemalloc.Traceback"><tt class="xref py py-class docutils literal"><span class="pre">Traceback</span></tt></a>
instance.</p>
</dd></dl>

</dd></dl>

</div>
<div class="section" id="trace">
<h3>27.7.2.7. Trace<a class="headerlink" href="#trace" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="tracemalloc.Trace">
<em class="property">class </em><tt class="descclassname">tracemalloc.</tt><tt class="descname">Trace</tt><a class="headerlink" href="#tracemalloc.Trace" title="Permalink to this definition">¶</a></dt>
<dd><p>Trace of a memory block.</p>
<p>The <a class="reference internal" href="#tracemalloc.Snapshot.traces" title="tracemalloc.Snapshot.traces"><tt class="xref py py-attr docutils literal"><span class="pre">Snapshot.traces</span></tt></a> attribute is a sequence of <a class="reference internal" href="#tracemalloc.Trace" title="tracemalloc.Trace"><tt class="xref py py-class docutils literal"><span class="pre">Trace</span></tt></a>
instances.</p>
<dl class="attribute">
<dt id="tracemalloc.Trace.size">
<tt class="descname">size</tt><a class="headerlink" href="#tracemalloc.Trace.size" title="Permalink to this definition">¶</a></dt>
<dd><p>Size of the memory block in bytes (<tt class="docutils literal"><span class="pre">int</span></tt>).</p>
</dd></dl>

<dl class="attribute">
<dt id="tracemalloc.Trace.traceback">
<tt class="descname">traceback</tt><a class="headerlink" href="#tracemalloc.Trace.traceback" title="Permalink to this definition">¶</a></dt>
<dd><p>Traceback where the memory block was allocated, <a class="reference internal" href="#tracemalloc.Traceback" title="tracemalloc.Traceback"><tt class="xref py py-class docutils literal"><span class="pre">Traceback</span></tt></a>
instance.</p>
</dd></dl>

</dd></dl>

</div>
<div class="section" id="traceback">
<h3>27.7.2.8. Traceback<a class="headerlink" href="#traceback" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="tracemalloc.Traceback">
<em class="property">class </em><tt class="descclassname">tracemalloc.</tt><tt class="descname">Traceback</tt><a class="headerlink" href="#tracemalloc.Traceback" title="Permalink to this definition">¶</a></dt>
<dd><p>Sequence of <a class="reference internal" href="#tracemalloc.Frame" title="tracemalloc.Frame"><tt class="xref py py-class docutils literal"><span class="pre">Frame</span></tt></a> instances sorted from the most recent frame to
the oldest frame.</p>
<p>A traceback contains at least <tt class="docutils literal"><span class="pre">1</span></tt> frame. If the <tt class="docutils literal"><span class="pre">tracemalloc</span></tt> module
failed to get a frame, the filename <tt class="docutils literal"><span class="pre">&quot;&lt;unknown&gt;&quot;</span></tt> at line number <tt class="docutils literal"><span class="pre">0</span></tt> is
used.</p>
<p>When a snapshot is taken, tracebacks of traces are limited to
<a class="reference internal" href="#tracemalloc.get_traceback_limit" title="tracemalloc.get_traceback_limit"><tt class="xref py py-func docutils literal"><span class="pre">get_traceback_limit()</span></tt></a> frames. See the <a class="reference internal" href="#tracemalloc.take_snapshot" title="tracemalloc.take_snapshot"><tt class="xref py py-func docutils literal"><span class="pre">take_snapshot()</span></tt></a> function.</p>
<p>The <a class="reference internal" href="#tracemalloc.Trace.traceback" title="tracemalloc.Trace.traceback"><tt class="xref py py-attr docutils literal"><span class="pre">Trace.traceback</span></tt></a> attribute is an instance of <a class="reference internal" href="#tracemalloc.Traceback" title="tracemalloc.Traceback"><tt class="xref py py-class docutils literal"><span class="pre">Traceback</span></tt></a>
instance.</p>
<dl class="method">
<dt id="tracemalloc.Traceback.format">
<tt class="descname">format</tt><big>(</big><em>limit=None</em><big>)</big><a class="headerlink" href="#tracemalloc.Traceback.format" title="Permalink to this definition">¶</a></dt>
<dd><p>Format the traceback as a list of lines with newlines.  Use the
<a class="reference internal" href="linecache.html#module-linecache" title="linecache: This module provides random access to individual lines from text files."><tt class="xref py py-mod docutils literal"><span class="pre">linecache</span></tt></a> module to retrieve lines from the source code.  If
<em>limit</em> is set, only format the <em>limit</em> most recent frames.</p>
<p>Similar to the <a class="reference internal" href="traceback.html#traceback.format_tb" title="traceback.format_tb"><tt class="xref py py-func docutils literal"><span class="pre">traceback.format_tb()</span></tt></a> function, except that
<a class="reference internal" href="functions.html#format" title="format"><tt class="xref py py-meth docutils literal"><span class="pre">format()</span></tt></a> does not include newlines.</p>
<p>Example:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="nb">print</span><span class="p">(</span><span class="s">&quot;Traceback (most recent call first):&quot;</span><span class="p">)</span>
<span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">traceback</span><span class="p">:</span>
    <span class="nb">print</span><span class="p">(</span><span class="n">line</span><span class="p">)</span>
</pre></div>
</div>
<p>Output:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="n">Traceback</span> <span class="p">(</span><span class="n">most</span> <span class="n">recent</span> <span class="n">call</span> <span class="n">first</span><span class="p">):</span>
  <span class="n">File</span> <span class="s">&quot;test.py&quot;</span><span class="p">,</span> <span class="n">line</span> <span class="mi">9</span>
    <span class="n">obj</span> <span class="o">=</span> <span class="n">Object</span><span class="p">()</span>
  <span class="n">File</span> <span class="s">&quot;test.py&quot;</span><span class="p">,</span> <span class="n">line</span> <span class="mi">12</span>
    <span class="n">tb</span> <span class="o">=</span> <span class="n">tracemalloc</span><span class="o">.</span><span class="n">get_object_traceback</span><span class="p">(</span><span class="n">f</span><span class="p">())</span>
</pre></div>
</div>
</dd></dl>

</dd></dl>

</div>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../contents.html">Table Of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">27.7. <tt class="docutils literal"><span class="pre">tracemalloc</span></tt> &#8212; Trace memory allocations</a><ul>
<li><a class="reference internal" href="#examples">27.7.1. Examples</a><ul>
<li><a class="reference internal" href="#display-the-top-10">27.7.1.1. Display the top 10</a></li>
<li><a class="reference internal" href="#compute-differences">27.7.1.2. Compute differences</a></li>
<li><a class="reference internal" href="#get-the-traceback-of-a-memory-block">27.7.1.3. Get the traceback of a memory block</a></li>
<li><a class="reference internal" href="#pretty-top">27.7.1.4. Pretty top</a></li>
</ul>
</li>
<li><a class="reference internal" href="#api">27.7.2. API</a><ul>
<li><a class="reference internal" href="#functions">27.7.2.1. Functions</a></li>
<li><a class="reference internal" href="#filter">27.7.2.2. Filter</a></li>
<li><a class="reference internal" href="#frame">27.7.2.3. Frame</a></li>
<li><a class="reference internal" href="#snapshot">27.7.2.4. Snapshot</a></li>
<li><a class="reference internal" href="#statistic">27.7.2.5. Statistic</a></li>
<li><a class="reference internal" href="#statisticdiff">27.7.2.6. StatisticDiff</a></li>
<li><a class="reference internal" href="#trace">27.7.2.7. Trace</a></li>
<li><a class="reference internal" href="#traceback">27.7.2.8. Traceback</a></li>
</ul>
</li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="trace.html"
                        title="previous chapter">27.6. <tt class="docutils literal"><span class="pre">trace</span></tt> &#8212; Trace or track Python statement execution</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="distribution.html"
                        title="next chapter">28. Software Packaging and Distribution</a></p>
<h3>This Page</h3>
<ul class="this-page-menu">
  <li><a href="../bugs.html">Report a Bug</a></li>
  <li><a href="../_sources/library/tracemalloc.txt"
         rel="nofollow">Show Source</a></li>
</ul>

<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>  
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="distribution.html" title="28. Software Packaging and Distribution"
             >next</a> |</li>
        <li class="right" >
          <a href="trace.html" title="27.6. trace — Trace or track Python statement execution"
             >previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="https://www.python.org/">Python</a> &raquo;</li>
        <li>
          <span class="version_switcher_placeholder">3.4.3</span>
          <a href="../index.html">Documentation</a> &raquo;
        </li>

          <li><a href="index.html" >The Python Standard Library</a> &raquo;</li>
          <li><a href="debug.html" >27. Debugging and Profiling</a> &raquo;</li> 
      </ul>
    </div>  
    <div class="footer">
    &copy; <a href="../copyright.html">Copyright</a> 1990-2015, Python Software Foundation.
    <br />
    The Python Software Foundation is a non-profit corporation.
    <a href="https://www.python.org/psf/donations/">Please donate.</a>
    <br />
    Last updated on Jun 15, 2015.
    <a href="../bugs.html">Found a bug</a>?
    <br />
    Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.2.3.
    </div>

  </body>
</html>